package models

import (
	"time"

	"github.com/astaxie/beego"
)

type goodsList struct {
	Id             int
	Title          string
	Description    string
	Thumb          string
	Stocks         int
	Sales          int
	Price          float64
	Commission     float64
	End_time       int
	Purchase_limit int
	Isadd          int
}

type List struct {
	Keystart int //下一页参数keystart 的值
	Hasnext  int //是否还有下一页
	List     interface{}
}

type Xxp_classify struct {
	Id      int
	Name    string
	Pid     int
	Sort    int
	IsShow  int
	Created int64
}

func GetGoodsList(uid, types, sort, order, keystart int, classify string) (res Response) {
	var lists List
	var goodslist []goodsList
	sql := "SELECT *,IF(g.id IN(SELECT gid FROM xxp_goods_kol b where b.uid=? ),1,0) as isadd FROM `xxp_goods` g where g.status > 0 "
	switch types {
	case 1:
		sql += " AND g.sales > 5 "
	case 2:
		sql += " AND g.types = 1 "
	case 3:
		sql += " AND g.types = 2 "
	case 4:
		sql += " AND g.types = 3 "
	}
	if classify != "" {
		sql += " AND  CONCAT(',',classify_ids,',') like '%," + classify + ",%'  "
	}
	switch sort {
	case 1:
		sql += " ORDER BY g.created "
	case 2:
		sql += " ORDER BY g.commission "
	case 3:
		sql += " ORDER BY g.price "
	}
	if order == 1 {
		sql += " ASC "
	} else {
		sql += " DESC "
	}
	ormer.Raw(sql+"  LIMIT ?,30", uid, 30*keystart).QueryRows(&goodslist)
	keystart = keystart + 1
	if len(goodslist) <= 0 {
		lists.List = []string{}
	} else {
		lists.List = goodslist
	}
	//判断是否有下一页
	var a []goodsList
	n, _ := ormer.Raw(sql+"  LIMIT ?,30", uid, 30*keystart).QueryRows(&a)
	if n > 0 {
		lists.Hasnext = 1
	}
	lists.Keystart = keystart
	res.Data = lists
	return
}

type goodsDetail struct {
	Xxp_goods
	Isadd    int
	Schedule []Xxp_schedule
}

func GetGoodsDetail(uid, gid int) (res Response) {
	var good Xxp_goods
	var data goodsDetail
	err := ormer.Raw("SELECT * FROM `xxp_goods` g where g.id = ? ", gid).QueryRow(&good)
	beego.Debug(err)
	if err != nil {
		res.Code = 1000400
		res.Msg = "数据不存在"
		return
	}
	var id int
	ormer.Raw("select id from xxp_goods_kol where gid = ? AND uid = ?", gid, uid).QueryRow(&id)
	if id > 0 {
		data.Isadd = 1
	} else {
		data.Isadd = 0
	}
	good.Base_price = 0
	data.Xxp_goods = good

	var schedule []Xxp_schedule
	ormer.Raw("SELECT * FROM `xxp_schedule` WHERE gid=? ORDER BY sort ASC", gid).QueryRows(&schedule)
	data.Schedule = schedule

	res.Data = data
	return
}

func GetGoodsAdd(uid, gid int) (res Response) {
	_, err := ormer.Raw("INSERT INTO `xxp_goods_kol`(`uid`, `gid`, `created`) VALUES (?,?,?)", uid, gid, time.Now().Unix()).Exec()
	if err != nil {
		res.Code = 1000100
		res.Msg = "添加失败"
	} else {
		res.Msg = "添加成功"
	}
	return
}

func GetGoodsDel(uid, gid int) (res Response) {
	_, err := ormer.Raw("DELETE FROM `xxp_goods_kol` WHERE uid=? AND gid=?", uid, gid).Exec()
	if err != nil {
		res.Code = 1000200
		res.Msg = "删除失败"
	} else {
		res.Msg = "删除成功"
	}
	return
}

type classify struct {
	Xxp_classify
	Child []Xxp_classify
}

func GetGoodsClassify() (res Response) {
	var list []Xxp_classify
	var father []classify
	ormer.Raw("SELECT * FROM `xxp_classify` WHERE `is_show` = 1 ORDER BY sort desc").QueryRows(&list)
	for _, v := range list {
		if v.Pid == 1 {
			father = append(father, classify{
				Xxp_classify: v,
			})
		}
	}

	for k, v := range father {
		for _, v0 := range list {
			if v0.Pid == v.Xxp_classify.Id {
				father[k].Child = append(father[k].Child, v0)
			}
		}
	}
	if len(father) <= 0 {
		res.Data = []string{}
	} else {
		res.Data = father
	}
	return
}
